<?php
//----------------------------------------------------------------------------//
// Class              : Ctl_Sanpham											  //
// Modification       :					  //
//----------------------------------------------------------------------------//
// Date(yyyy.mm.dd)   Developer                       Action				  //
//----------------------------------------------------------------------------//
// 2008.01.21         Oanhhtk                     Created				  //
//----------------------------------------------------------------------------//

class Ctl_Sanpham extends ControlBase{
    var $modul="content";
	//==========================================================================
	// Function    : Ctl_Sanpham
	// Argument    :
	// ReturnValue :
	// Creator     : Oanhhtk 2007.03.12
	// Details     : AppendData record of table
	//==========================================================================
	function Ctl_Sanpham(){
	   	ControlBase::ControlBase();
	}

	//==========================================================================
	// Function    :  execute
	// Argument    :
	// ReturnValue :
	// Creator     : Oanhhtk 2007.03.12
	// Details     : AppendData record of table
	//==========================================================================
 	function execute($action=""){
	    global $db;
	    global     $arrayError,$_POST, $objModel;
	    $arrayData =  array();
		$arrayData1 =  array();
		$arrayData2 =  array();
	    $this->modul = CM_getParameter(MODUL);
 		if ($action==""){
 			$action = CM_getParameter(ACTION);
 		}
		$listField="*";
		// Get data
		include_once(DIR_MODEL."md_san_pham.class.php");
		$objModel = new MD_Sanpham($this->db);
 		switch($action){
			case "SearchCatContent":
				
				$id = CM_getParameter("id");
				if(trim($id)!=""){
					$arrayData = $objModel->getByWhere($listField,"cat_id=".$id." and shows=1","id desc");
				}
				$cat_id = CM_getParameter("cat_id");
				include_once(DIR_MODEL."md_cat_content.class.php");
				$objcatContent = new MD_CatContent($db);
				$arrayData_catContent =  array();      
				$arrayData_catContent = $objcatContent->getById($cat_id);
				//print_r($arrayData_catContent);
  			    
				include_once(DIR_VIEW."v_content_list.php");
				V_ContentList($arrayData,$action,$arrayData_catContent);
				break;
			case "SearchText":
				$ten_sp = isset($_POST['ten_sp'])?$_POST['ten_sp']:(isset($_SESSION['ten_sp'])?$_SESSION['ten_sp']:"");
				$loai_sp = isset($_POST['loai_sp'])?$_POST['loai_sp']:(isset($_SESSION['loai_sp_search'])?$_SESSION['loai_sp_search']:0);
				$page = (isset($_GET["curPage"]) && is_numeric($_GET["curPage"]))?$_GET["curPage"]:1;
				$sort = $_GET["sort"];
				$_SESSION['ten_sp'] = $ten_sp;
				$_SESSION['loai_sp_search'] = $loai_sp;
				include_once(DIR_MODEL."md_cat_san_pham.class.php");
				$objcatSanpham = new MD_CatSanpham($db);
				if($loai_sp != 0){
					$arrayDataCatSP = $objcatSanpham -> getByWhere("*","parent_id = ".$loai_sp,"");					
				}
				if(!empty($arrayDataCatSP)){
					$arrayTemp  = array();
					$i = 0;
					foreach($arrayDataCatSP as $rowCat){
						$arrayTemp[$i] = $rowCat['id'];
						$i ++;
					}
					$listCatSP  = $loai_sp.",";
					$listCatSP .= implode(",",$arrayTemp);
				}
				$_SESSION['listCatSP_Hidden'] = $listCatSP;
				include_once("../includes/i_timkiem.php");
				echo "<div id=\"searchAdvanceForm\"></div><div>&nbsp;</div>";
				include_once("../view/v_select_search_sort.php"); 
				
				echo "<DIV id=p_loading  style=\"width:300px; height:70px; background-color:#FFFFD7 ;
      position: absolute; left: 600px; top: 330px; text-align:center;\">
     <img src=\"../images/loading.gif\">
     <STRONG>
      <FONT color=#ff0000>Đang tải dữ liệu....<BR></FONT>
      <FONT color=#0000ff>&nbsp;Xin vui lòng chờ trong giây lát.</FONT>
     </STRONG>
    </DIV>
					<script>jQuery.get(\"".DIR_MODEL."md_search_ajax.php\",{ten_sp:\"".$ten_sp."\",loai_sp_search:\"".$loai_sp."\",list_loai_sp:\"".$listCatSP."\",curPage:\"".$page."\",sort:\"".$sort."\"},function(data){jQuery(\"#p_loading\").hide(\"slow\");jQuery(\"#p_ketqua\").html(data);jQuery(\"#p_ketqua\").show(\"slow\")});</script>
					<p id=\"p_ketqua\" style=\"display:none\"></p>
				";
				/*$arrayData = $objModel->getForSearchText($ten_sp,$loai_sp,$listCatSP);
				include_once(DIR_VIEW."v_san_pham_list_search.php");
				V_SanphamListSearch($arrayData,$action);*/
				break;
			case "search_advance":
				include_once("../includes/i_timkiem_advance.php");
				break;
			case "search_advance_text":
				$ten_sp = isset($_POST['ten_sp_adv'])?$_POST['ten_sp_adv']:(isset($_SESSION['ten_sp'])?$_SESSION['ten_sp']:"");
				$loaisp = isset($_POST['loai_sp_adv'])?$_POST['loai_sp_adv']:(isset($_SESSION['loai_sp_search'])?$_SESSION['loai_sp_search']:0);
				$sanpham = isset($_POST['sanpham_adv'])?$_POST['sanpham_adv']:(isset($_SESSION["sanpham_adv"])?$_SESSION["sanpham_adv"]:0);
				$price_from = isset($_POST['price_from_adv'])?$_POST['price_from_adv']:(isset($_SESSION["price_from_adv"])?$_SESSION["price_from_adv"]:"");
				$price_to = isset($_POST['price_to_adv'])?$_POST['price_to_adv']:(isset($_SESSION["price_to_adv"])?$_SESSION["price_to_adv"]:"");
				$hangsx = isset($_POST['hangsx_adv'])?$_POST["hangsx_adv"]:(isset($_SESSION["hangsx_adv"])?$_SESSION["hangsx_adv"]:0);
				$page = (isset($_GET["curPage"]) && is_numeric($_GET["curPage"]))?$_GET["curPage"]:1;
				include_once(DIR_MODEL."md_cat_san_pham.class.php");
				$objcatSanpham = new MD_CatSanpham($db);
				if($loaisp != 0){
					$arrayDataCatSP = $objcatSanpham -> getByWhere("*","parent_id = ".$loaisp,"");					
				}
				if(!empty($arrayDataCatSP)){
					$arrayTemp  = array();
					$i = 0;
					foreach($arrayDataCatSP as $rowCat){
						$arrayTemp[$i] = $rowCat['id'];
						$i ++;
					}
					$listCatSP  = $loaisp.",";
					$listCatSP .= implode(",",$arrayTemp);
				}
				$sort = $_GET["sort"];
				$_SESSION['listCatSP_Hidden'] = $listCatSP;
				if($price_from !=''){
					$price_from_temp = explode(",",$price_from);
					$price_from = implode("",$price_from_temp);
				}
				if($price_to !=''){
					$price_to_temp = explode(",",$price_to);
					$price_to = implode("",$price_to_temp);
				}
				$_SESSION['ten_sp'] = $ten_sp;
				$_SESSION['loai_sp_search'] = $loaisp;
				$_SESSION['sanpham_adv'] = $sanpham;
				$_SESSION['price_from'] = $price_from;
				$_SESSION['price_to'] = $price_to;
				$_SESSION['hangsx'] = $hangsx;
				include_once("../includes/i_timkiem.php");
				
				echo "<DIV id=p_loading  style=\"width:300px; height:70px; background-color:#FFFFD7 ;
      position: absolute; left: 600px; top: 330px; text-align:center;\">
     <img src=\"../images/loading.gif\">
     <STRONG>
      <FONT color=#ff0000>Đang tải dữ liệu....<BR></FONT>
      <FONT color=#0000ff>&nbsp;Xin vui lòng chờ trong giây lát.</FONT>
     </STRONG>
    </DIV><script>jQuery.get(\"".DIR_MODEL."md_search_ajax.php\",{ten_sp:\"".$ten_sp."\",loai_sp_search:\"".$loaisp."\",list_loai_sp:\"".$listCatSP."\",curPage:\"".$page."\",s_a:\"1\",sanpham:\"".$sanpham."\",price_from:\"".$price_from."\",price_to:\"".$price_to."\",hangsx:\"".$hangsx."\",action:\"".$action."\",sort:\"".$sort."\"},function(data){jQuery(\"#p_loading\").hide(\"slow\");jQuery(\"#p_ketqua\").html(data);jQuery(\"#p_ketqua\").show(\"slow\")});</script>
				<p id=\"p_ketqua\" style=\"display:none\"></p>
				";
				/*
				$arrayData = $objModel-> getForSearchAdvanceText($nameTitle,$loaisp,$sanpham,$price_from,$price_to,$hangsx,$listCatSP);
				include_once(DIR_VIEW."v_san_pham_list_search_advance.php");
				V_SanphamListSearchAdvance($arrayData,$action);*/
				break;
			case "View":
				//echo "vao ham view";
				include_once(DIR_VIEW."v_san_pham_view.php");
				$id = CM_getParameter("id");
				$id_loai_sp = CM_getParameter("id_loai_sp");
                $arrayData = $objModel->getFullById($id);
				
                if(empty($arrayData)){
                    $arrayError[]="Không tìm thấy bản ghi nào";
                }
				//unset($arrayData,$arrayData_catContent);
				
				//$this->updateCountClick($id);
				
                $arrayData2 = $objModel->getByWhere($listField,"id<>'".$id."' and id_loai_sp=".$id_loai_sp." and hien_thi=1","thu_tu asc limit 5");
                  // show data
				if ($objModel->errno()){
                    $arrayError[]=$LANG["error"]["khong_tim_thay_ban_ghi"];
				}
				V_SanphamView($arrayData,$arrayData2,"View");
				//V_ContentList2($arrayData2,$arrayData_catContent);
				//V_ContentList2($arrayData2);
				
				break;
			case "ListNew":
				
				include_once("../includes/i_timkiem.php");
				echo "<div id=\"searchAdvanceForm\"></div><div>&nbsp;</div>";
				include_once("../view/v_select_sort_new.php"); 
				if(CM_getParameter("sort")!=''){
				$arrayData = $objModel -> getByWhere("*","hien_thi = 1 AND gia_sp_km = 0","gia_sp ".CM_getParameter("sort"));
				include_once(DIR_VIEW."getsortnew.php");
				}else{
				$arrayData = $objModel -> getByWhere("*","hien_thi = 1 AND gia_sp_km = 0","gia_sp desc");
				include_once(DIR_VIEW."v_san_pham_list_new.php");
				V_SanphamListNew($arrayData,$action);
				}
				
				break;
			case "ListTopSell":
				$arrayData = $objModel -> getByWhere("*","hien_thi = 1 AND solanban > 0","solanban desc");
				include_once("../includes/i_timkiem.php");	
				echo "<div id=\"searchAdvanceForm\"></div><div>&nbsp;</div>";			
				include_once(DIR_VIEW."v_san_pham_list_top_sell.php");
				V_SanphamListTopSell($arrayData,$action);
				break;
			case "List":
			default :{
				$id_loai_sp = CM_getParameter("id_loai_sp");				
				include_once(DIR_MODEL."md_cat_san_pham.class.php");
				$objcatSanpham = new MD_CatSanpham($db);
				$arrayData_catSanpham =  array();      
				$arrayData_catSanpham = $objcatSanpham->getByWhere("*","id = ".$id_loai_sp, "");		
				include_once("../includes/i_timkiem.php");
				echo "<div id=\"searchAdvanceForm\"></div><div>&nbsp;</div>";
				include_once("../view/v_list_menu.php"); 				
				include_once("../view/v_select_sort.php"); 
				if(CM_getParameter("sort")!=''){
					$arrayData = $objModel -> getByWhere("*","hien_thi=1 and id_loai_sp=".$id_loai_sp."","gia_sp ".CM_getParameter("sort"));
					include_once(DIR_VIEW."getsort.php");
				}else{
					$arrayData = $objModel->getByWhere($listField,"hien_thi=1 and id_loai_sp=".$id_loai_sp."","gia_sp desc"); 
					include_once(DIR_VIEW."v_san_pham_list.php");
					V_SanphamList($arrayData,$action,$arrayData_catSanpham); 
				}		
					
							
				
				
			}
 		}
        unset($arrayData,$arrayData1, $arrayData2,$arrayData_catSanpham);
	}
	//==========================================================================
	// Function    :  update
	// Argument    :
	// ReturnValue :
	// Creator     : ThanhTX  2007.07.12
	// Details     : AppendData record of table
	//==========================================================================
 	function updateCountClick($id)
	{
        global     $arrayError,$_POST;	
   		$objModel = new MD_Content($this->db);
   		$objModel->updateCountClick(&$id);
     	if ($objModel->errno())
		{
             $arrayError[]= $objModel->error();
             return false;
     	}
	}
}
?>